(function ($, window, document) {
    setTimeout(function () {
        $.each($(".owl-controls").find(".owl-page"), function (index, data) {
            var thisControl = $(data);
            var itemIndex = $(data).parents("#owl-slider").find(".owl-item")[index];
            thisControl.attr("imgsrc", $(itemIndex).find("img").attr("src"));
        });
    }, 600);
    ///Slide Show
    $.fn.swipeSlideShow = function (options) {
        var defaults = { "slideItem": ".slide-item", "thumbNail": true, "dragMouse": false };
        var settings = $.extend(defaults, options || {});
        var slideWrapper = $(this);
        var slidePager = $("<ol/>", { "class": "slide-pager" });
        $.each($(this), function () {
            $(this).append(slidePager);
            $.each($(this).find(settings.slideItem), function (index, data) {
                $(this).attr("index", index);
                slideWrapper.find(slidePager).append("<li class='pager-item' index='" + index + "'><a href='#'></a></li>");
            });
            $(this).find(settings.slideItem).first().addClass("active");
            $(this).find(".pager-item").first().addClass("active");
        });
        $(".pager-item").on("click", function (e) {
            e.preventDefault() ? e.preventDefault() : e.returnValue;
            $(".pager-item").removeClass("active");
            slideWrapper.find(settings.slideItem).fadeOut(300);
            var thisIndex = $(this).attr("index");
            $(this).addClass("active");
            var slideItemW = slideWrapper.find(settings.slideItem).width();
            slideWrapper.find(settings.slideItem + "[index='" + thisIndex + "']").css({ "left": slideItemW });
            slideWrapper.find(settings.slideItem + "[index='" + thisIndex + "']").show(0, function () {
                $(this).animate({ "left": "0" });
            });
        });
        $.slideThumB = function () {
            var thumB = $('<div/>', {
                "class": "slide-thumb"
            });
            $(".pager-item").on("mouseover", function () {
                var virArrow = $('<div/>', {
                    "class": "slide-thumb-arrow"
                });
                var thisIndex = $(this).attr("index");
                var imgThumB = $('<img/>', {
                    "src": slideWrapper.find(settings.slideItem + "[index='" + thisIndex + "']").find("img").attr("src")
                });
                $(this).prepend(thumB);
                $(thumB).html(imgThumB).append(virArrow);
            });
            $(".pager-item").on("mouseout", function () {
                $(thumB).remove();
            });
        }
        if (settings.thumbNail === true) {
            $.slideThumB();
        }
        if (settings.dragMouse === true) {
            $(settings.slideItem).bind("mousedown", function (d) {
                var elementItem = $(this);
                d.preventDefault() ? d.preventDefault() : d.returnValue;
                var flag = 0;
                var mouseStart = d.pageX;
                $(this).on("mousemove", function (e) {
                    if (flag === 0) {
                        if (mouseStart > e.pageX) {
                            var slideItemW = $(this).width();
                            $(this).css({ "left": slideItemW });
                            $(this).css({ "left": e.pageX - $(this).offset().left });
                        }
                        else if (mouseStart < e.pageX) {
                            $(this).css({ "left": 0 });
                            $(this).css({ "left": e.pageX - $(this).offset().left });
                        }
                    }
                });
                $(this).bind("mouseup", function (e) {
                    flag += 1;
                    if (mouseStart > e.pageX) {
                        var slideItemW = slideWrapper.find(settings.slideItem).width();
                        if ($(this).next().length > 0) {
                            $(this).hide(0);
                            $(this).prev().hide(0);
                            $(this).next().css({ "left": slideItemW, "right": "auto" });
                            $(this).next().show(0, function () {
                                $(this).animate({ "left": "0" });
                            });
                        }
                        else {
                            $(this).hide(0);
                            $(this).prev().hide(0);
                            $(this).show(0, function () {
                                $(this).animate({ "left": 0, "right": "auto" });
                            });
                        }
                    }
                    else if (mouseStart < e.pageX) {
                        if ($(this).prev().length > 0) {
                            $(this).hide(0);
                            $(this).prev().css({ "right": 0, "left": "auto" });
                            $(this).prev().show(0, function () {
                                $(this).animate({ "right": slideItemW });
                            });
                        }
                        else {
                            $(this).hide(0);
                            $(this).css({ "right": 0, "left": "auto" });
                            $(this).show(0, function () {
                                $(this).animate({ "right": slideItemW, "left": "auto" });
                            });
                        }
                    }
                });
            });
        }
    }

    ///Chang Sprice
    $.changSprice = function (options) {
        var defaults = { "oldVal": "50" };
        var settings = $.extend(defaults, options || {});
        $.addSprice = function (value) {
            return "Giao dịch (" + value + "K)";
        }
        $(".split-btn-dropdown li").find(".chk").on("change", function () {
            if ($(this).is(":checked") === true) {
                var sprice = $.convertTextToInt($(this).siblings("p").text() + $(this).siblings(".outer-select-element").find(".select").val());
                var strSprice = $.convertTextToInt($(this).parents(".split-button").find(".btn-split").val());
                var newSp = $.addSprice(strSprice + sprice);
                $(this).parents(".split-button").find(".btn-split").val(newSp);
            }
            else {
                var sprice = $.convertTextToInt($(this).siblings("p").text() + $(this).siblings(".outer-select-element").find(".select").val());
                var strSprice = $.convertTextToInt($(this).parents(".split-button").find(".btn-split").val());
                var newSp = $.addSprice(strSprice - sprice);
                $(this).parents(".split-button").find(".btn-split").val(newSp);
            }
        });
        $(".split-btn-dropdown li").find(".select").on("change", function () {
            $(this).parent(".outer-select-element").siblings(".chk").prop("checked", "checked").attr("checked", "checked");
            var txtSprice = 0;
            var txtObjectPorSelect = $(".split-btn-dropdown").find("li").find(".chk:checked").siblings("p").add($(".split-btn-dropdown").find("li").find(".chk:checked").siblings(".outer-select-element").find(".select"));
            $.each(txtObjectPorSelect, function (index, value) {
                if (value.tagName === "SELECT") {
                    txtSprice += $.convertTextToInt($(value).find("option:selected").val());
                }
                else {
                    txtSprice += $.convertTextToInt($(value).text());
                }
                var newSp = $.addSprice(txtSprice + $.convertTextToInt(settings.oldVal));
                $(this).parents(".split-button").find(".btn-split").val(newSp);
            });
            var newSp = $.addSprice(txtSprice + $.convertTextToInt(settings.oldVal));
            $(this).parents(".split-button").find(".btn-split").val(newSp);
        });
    }
    $.fn.excuteComment = function (options) {
        var defaults = { "textArea": "#txtPostComment", "commentArea": ".comment-area >ul", "type": "comment", "minStrLength": 10 };
        var settings = $.extend(defaults, options || {});
        if (settings.type === "comment") {
            $(this).on("click", function () {
                var txtString = $(settings.textArea).val();
                if (txtString.length >= settings.minStrLength) {
                    var jsonStrComment = { "id": "0001", "avatar": "/Content/ImgResource/skyhigh1.jpg", "username": "Obame", "userlink": "/User/Index", "message": txtString, "parentid": "0", "time": "3:10 - 11/1/2014" };
                    var htmlStr = "";
                    htmlStr += '<li id="' + jsonStrComment.id + '">';
                    htmlStr += '<div class="avatar-commenter"><a href="' + jsonStrComment.userlink + '"><img src="' + jsonStrComment.avatar + '" /></a></div>';
                    htmlStr += '<div class="comment-detail"><div class="comment-arrow"></div><div class="comment-info">';
                    htmlStr += '<a href="' + jsonStrComment.userlink + '">' + jsonStrComment.username + '</a><label>vào lúc</label><span>' + jsonStrComment.time + '</span></div>';
                    htmlStr += '<p class="comment-content">' + jsonStrComment.message + '</p>';
                    htmlStr += '<div class="comment-detail-tool"><span><a class="link-reply" href="#">Trả lời</a></span></div></div>';
                    htmlStr += "</li>";
                    $(settings.commentArea).prepend(htmlStr);
                    $(settings.textArea).val("");
                    $(".link-reply").excuteReply({ "type": "reply" });
                    $(".comment-control").alertComment({ "active": false });
                }
                else {
                    $(".comment-control").alertComment({ "text": "Chiều dài comment phải lớn hơn " + settings.minStrLength + " ký tự" });
                }
            });
        }
        else if (settings.type === "reply") {
            $(this).on("keyup", function (e) {
                e.preventDefault() ? e.preventDefault() : e.returnValue;
                if (e.keyCode === 13 || e.which === 13) {
                    var txtString = $(this).val();
                    if (txtString.length > 2) {
                        var jsonStrComment = { "id": "0001", "avatar": "/Content/ImgResource/skyhigh1.jpg", "username": "Obame", "userlink": "/User/Index", "message": txtString, "parentid": "0", "time": "3:10 - 11/1/2014" };
                        var htmlStr = "";
                        htmlStr += "<li id=" + jsonStrComment.id + ">";
                        htmlStr += '<div class="reply-arrow"></div><div class="avatar-commenter"><a href="' + jsonStrComment.userlink + '">';
                        htmlStr += '<img src="' + jsonStrComment.avatar + '"/></a></div>';
                        htmlStr += '<div class="comment-detail">';
                        htmlStr += '<div class="comment-arrow"></div><div class="comment-info">';
                        htmlStr += "<a href='" + jsonStrComment.userlink + "'>" + jsonStrComment.username + "</a><label>vào lúc</label><span>" + jsonStrComment.time + "</span>";
                        htmlStr += '</div><p class="comment-content">' + jsonStrComment.message;
                        htmlStr += "</p></div>";
                        htmlStr += "</li>";
                        htmlStr += "</ul>";
                        if ($(this).parent().parent("li").find("ul").length === 0) {
                            $(this).parent().after('<ul></ul>');
                            $(this).parent().parent("li").find("ul").append(htmlStr);
                        }
                        else {
                            $(this).parent().parent("li").find("ul").append(htmlStr);
                        }
                        $(this).val("");
                        $(this).parent().remove();
                    }
                }
            });
        }
    }
    $.fn.excuteReply = function () {
        $(this).on("click", function (e) {
            e.preventDefault() ? e.preventDefault() : e.returnValue;
            var inputBox = '<div class="reply-input-box"><textarea title="Trả lời ý kiến..." placeholder="Trả lời ý kiến..." aria-expanded="false" class="txt-reply"></textarea></div>';
            $(".reply-input-box").remove();
            $(this).parents(".comment-detail").after(inputBox);
            $(".txt-reply").excuteComment({ "type": "reply" });
        });
    }
    $.fn.alertComment = function (options) {
        var defaults = { "text": "Lỗi!", "active": true };
        var settings = $.extend(defaults, options || {});
        if (settings.active) {
            $(this).find(".alert-comment").remove();
            $(this).append('<p class="alert-comment"></p>');
            $(this).find(".alert-comment").text(settings.text);
        }
        else {
            $(this).find(".alert-comment").remove();
        }
    }
    //Load more comment
    var jsonComment = [
        { "id": "0001", "avatar": "/Content/ImgResource/skyhigh1.jpg", "username": "Obame", "userlink": "/User/Index", "message": "Lam te qua", "parentid": "0", "time": "3:10 - 11/1/2014" },
        { "id": "0002", "avatar": "/Content/ImgResource/avatar.png", "username": "Trung Lê", "userlink": "/User/Index", "message": "O, tu lam di", "parentid": "0001", "time": "3:10 - 11/1/2014" },
        { "id": "0003", "avatar": "/Content/ImgResource/avatar.png", "username": "Trung Lê", "userlink": "/User/Index", "message": "chem ha may", "parentid": "0001", "time": "3:10 - 11/1/2014" },
        { "id": "0004", "avatar": "/Content/ImgResource/avatar.png", "username": "Trung Lê", "userlink": "/User/Index", "message": "Ngon thi hen ra chua quanh lon", "parentid": "0001", "time": "3:10 - 11/1/2014" },
        { "id": "0005", "avatar": "/Content/ImgResource/skyhigh1.jpg", "username": "Binladen", "userlink": "/User/Index", "message": "Lam te qua may oi", "parentid": "0", "time": "3:10 - 11/1/2014" },
        { "id": "0006", "avatar": "/Content/ImgResource/avatar.png", "username": "Trung Lê 1", "userlink": "/User/Index", "message": "Shit", "parentid": "0005", "time": "3:10 - 11/1/2014" },
        { "id": "0007", "avatar": "/Content/ImgResource/avatar.png", "username": "Trung Lê 1", "userlink": "/User/Index", "message": "Ke tao", "parentid": "0005", "time": "3:10 - 11/1/2014" },
        { "id": "0008", "avatar": "/Content/ImgResource/avatar.png", "username": "Trung Lê 1", "userlink": "/User/Index", "message": "Dam phat chet lien", "parentid": "0005", "time": "3:10 - 11/1/2014" },
        { "id": "0009", "avatar": "/Content/ImgResource/skyhigh1.jpg", "username": "Trung Lê", "userlink": "/User/Index", "message": "Haizz", "parentid": "0001", "time": "3:10 - 11/1/2014" },
        { "id": "0010", "avatar": "/Content/ImgResource/skyhigh1.jpg", "username": "Trung Lê", "userlink": "/User/Index", "message": "Yep", "parentid": "0001", "time": "3:10 - 11/1/2014" }
    ];
    $.fn.loadMoreComment = function (options) {
        var defaults = { "wrapComment": ".comment-area >ul" };
        var settings = $.extend(defaults, options || {});
        $(this).on("click", function (e) {
            e.preventDefault() ? e.preventDefault() : e.returnValue;
            //$(settings.wrapComment);
            for (var index in jsonComment) {
                if (jsonComment[index].parentid === "0") {
                    var liHTML = "";
                    liHTML += "<li id=" + jsonComment[index].id + ">";
                    liHTML += '<div class="avatar-commenter"><a href="' + jsonComment[index].userlink + '"><img src="' + jsonComment[index].avatar + '" alt="test" /></a></div>';
                    liHTML += '<div class="comment-detail"><div class="comment-arrow"></div>';
                    liHTML += '<div class="comment-info">';
                    liHTML += '<a href="' + jsonComment[index].userlink + '">' + jsonComment[index].username + '</a><label>vào lúc</label><span>' + jsonComment[index].time + '</span></div>';
                    liHTML += '<p class="comment-content">' + jsonComment[index].message + '</p>';
                    liHTML += '<div class="comment-detail-tool"><span><a class="link-reply" href="#">Trả lời</a></span></div></div>';
                    liHTML += "</li>";
                    $(settings.wrapComment).append(liHTML);
                    var thisArr = $.getObjectbyAttr(jsonComment, "parentid", jsonComment[index].id);
                    if (thisArr.length > 0) {
                        var ulChild = "";
                        ulChild += "<ul>";
                        for (var cIndex = 0, len = thisArr.length; cIndex < len; cIndex++) {
                            if (thisArr[cIndex].parentid === jsonComment[index].id) {
                                ulChild += "<li id=" + thisArr[cIndex].id + ">";
                                ulChild += '<div class="reply-arrow"></div><div class="avatar-commenter"><a href="' + thisArr[cIndex].userlink + '">';
                                ulChild += '<img src="' + thisArr[cIndex].avatar + '"/></a></div>';
                                ulChild += '<div class="comment-detail">';
                                ulChild += '<div class="comment-arrow"></div><div class="comment-info">';
                                ulChild += "<a href='" + thisArr[cIndex].userlink + "'>" + thisArr[cIndex].username + "</a><label>vào lúc</label><span>" + thisArr[cIndex].time + "</span>";
                                ulChild += '</div><p class="comment-content">' + thisArr[cIndex].message;
                                ulChild += "</p></div>";
                                ulChild += "</li>";
                            }
                        }
                        $("#" + jsonComment[index].id).append(ulChild);
                    }
                }
                else {
                    continue;
                }
            }
        });
    }
} (jQuery, window, document));


(function($){
	$(window).on("load",function(e) {
		$(".select-count-order, .select-each-count").cusSelect();
		$("#post-slider").swipeSlideShow();
        $(".pager-control #load-more").loadMoreComment();
		$.changSprice();
        $(".select").cusSelect();
        $("#btnPostComment").excuteComment();
        $(".link-reply").excuteReply();
    });
}(jQuery));
